<?php
include "config.php";
db_connection();
function checkSession($username,$password) {
	$value = $username.$password;
	$isToken = md5($value);
	$query = "SELECT * FROM sessioninfo WHERE token = '$isToken'";
	$result = mysql_query($query) or die (mysql_error());
	if(mysql_num_rows($result) == 1) {
		return true;
	}
	else {
		return false;
	}
}
function getToken($username,$password) {
	$value = $username.$password;
	$isToken = md5($value);
	return $isToken;
}
function writeSession($username,$password) {
	$lifetime = get_cfg_var("session.gc_maxlifetime");
	$expiration = time() + $lifetime;
	$value = $username.$password;
	$isToken = md5($value);
	
	$query = "INSERT INTO sessioninfo VALUES ('$SID','$expiration','$value','$isToken')";
	$result = mysql_query ($query);
	
	if (!$result) {
			$query = "UPDATE sessioninfo SET expiration = '$expiration', value = '$value' WHERE SID = '$SID' AND expiration >".time();
			$result = mysql_query ($query);
	}
	return $isToken;
}
function deleteSession($token) {
	$query = "DELETE FROM sessioninfo WHERE token = '$token'";
	$result = mysql_query ($query);
}
db_close();
?>